package com.cloud.elm.controller;

import com.cloud.elm.po.Food;
import com.cloud.elm.service.FoodService;
import com.cloud.elm.utils.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/elm/FoodController")
public class FoodController {
	
	@Autowired
	private FoodService foodService;

	@GetMapping("/listFoodByBusinessId/{businessId}")
	public ResultBody<List<Food>> listFoodByBusinessId(@PathVariable("businessId") String businessId) {
		return ResultBody.success(foodService.listFoodByBusinessId(businessId));
	}

	@RequestMapping("/business/food/{foodId}")
	public ResultBody<Food> getFoodByFoodId(@PathVariable Integer foodId) {
		try {
			Food food = foodService.getFoodById(foodId);
			if (food == null) {
				return ResultBody.error("不存在该商品", null);
			}
//			FoodVO foodVO = FoodVO.builder()
//					.foodName(food.getFoodName())
//					.foodPrice(food.getFoodPrice())
//					.foodImg(food.getFoodImg())
//					.foodExplain(food.getFoodExplain())
//					.remarks(food.getRemarks()).build();
			return ResultBody.success("获取成功！", food);
		} catch (Exception e) {
			return ResultBody.error("获取失败！", null);
		}
	}

	@PostMapping("/addFoodByBusinessId")
	public ResultBody<Object> addFoodByBusinessId(Food food) {
		try {
			if (food.getFoodName() == null || food.getFoodPrice() == null || food.getBusinessId() == null) {
				return ResultBody.error("商品信息不完整", null);
			}
			foodService.addFoodByBusinessId(food);
			return ResultBody.success("新建商品成功", null);
		} catch (Exception e) {
			return ResultBody.error("新建商品失败", null);
		}
	}

	@DeleteMapping("/delFoodById/{foodId}/{businessId}")
	public ResultBody<Object> delFoodById(@PathVariable("foodId") Integer foodId,
										  @PathVariable("businessId") String businessId) {
		try {
			Food food = foodService.getFoodById(foodId);
			if (food == null) {
				return ResultBody.error("该商品不存在",null);
			}
			if (!food.getBusinessId().equals(businessId)){
				return ResultBody.error("这不是你的商品", null);
			}

			foodService.delFoodById(foodId);
			return ResultBody.success("删除成功",null);
		} catch (Exception e) {
			return ResultBody.error("删除失败", null);
		}
	}

	@PutMapping("/updateFoodById")
	public ResultBody<Object> updateFoodById(@RequestParam Integer foodId,
											 @RequestParam String foodName,
											 @RequestParam String foodExplain,
											 @RequestParam String foodImg,
											 @RequestParam Double foodPrice,
											 @RequestParam String businessId,
											 @RequestParam String remarks) {
		try {
			Food food = foodService.getFoodById(foodId);
			if (food == null) {
				return ResultBody.error("该商品不存在", null);
			}
			if (!food.getBusinessId().equals(businessId)) {
				return ResultBody.error("这不是你的商品");
			}
			food.setFoodName(foodName);
			food.setFoodExplain(foodExplain);
			food.setFoodImg(foodImg);
			food.setFoodPrice(foodPrice);
			food.setRemarks(remarks);

			if (food.getFoodName() == null || food.getFoodPrice() == null || food.getFoodId() == null || food.getBusinessId() == null) {
				return ResultBody.error("未完成填写", null);
			}

			foodService.updateFoodById(food);
			return ResultBody.success("修改成功", null);
		} catch (Exception e) {
			return ResultBody.error("修改失败", null);
		}
	}

}
